package com.red.servlet.house;

import com.red.service.impl.HouseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "HouseDeleteServlet", value = "/deleteHouse")

public class HouseDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String[] sIds = req.getParameterValues("house_id");

        ArrayList<Integer> ids = new ArrayList<>();

        for(String id : sIds){
            try{
                ids.add(Integer.parseInt(id));
            }catch (NumberFormatException e){
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST,"id序列中包含不符要求的数据");
                break;
            }
        }

        int count = new HouseServiceImpl().deleteHouse(ids);

        resp.setStatus(HttpServletResponse.SC_OK);
        resp.getWriter().print(count);
    }
}
